#include "XMLBaseProto.h"

/////////////////////////////////////////////////////////////////////
XMLReqReader::XMLReqReader(void)
{
}


XMLReqReader::~XMLReqReader(void)
{
}
int XMLReqReader::read(const char* str,CommRequest& req)
{
	m_pDoc->Parse(str);
	//if (!m_pDoc->Parse(str))
	//	return -1;
	//TiXmlHandle docHandle(&m_doc);
	TiXmlDeclaration* pDec = m_pDoc->FirstChild()->ToDeclaration();

	req.version = pDec->Version();
	req.encoding = pDec->Encoding();

	m_pRootEle = m_pDoc->RootElement();
	TiXmlElement* pChild = m_pRootEle->FirstChildElement("Security");
	req.ip   = pChild->Attribute("ip");
	req.date = pChild->Attribute("date");

	TiXmlElement* pChild2 = m_pRootEle->FirstChildElement("Service");
	//const char* strBusiness = pChild2->Attribute("business");
	//const char* strFunc = pChild2->Attribute("function");

	return read(req);
}
/////////////////////////////////////////////////////////////////////
XMLRepReader::XMLRepReader(void)
{
}


XMLRepReader::~XMLRepReader(void)
{
}
int XMLRepReader::read(const char* str,CommReply& rep)
{
	m_pDoc->Parse(str);
	//TiXmlHandle docHandle(&m_doc);
	TiXmlDeclaration* pDec = m_pDoc->FirstChild()->ToDeclaration();

	rep.version = pDec->Version();
	rep.encoding = pDec->Encoding();

	m_pRootEle = m_pDoc->RootElement();
	TiXmlElement* pChild = m_pRootEle->FirstChildElement("ResultInfo");
	pChild->Attribute("resultcode",&rep.status);
	rep.errmsg = pChild->Attribute("errormessage");

	TiXmlElement* pChild2 = m_pRootEle->FirstChildElement("Service");
	//const char* strBusiness = pChild2->Attribute("business");
	//const char* strFunc = pChild2->Attribute("function");

	return read(rep);
}
/////////////////////////////////////////////////////////////////////
XMLReqWriter::XMLReqWriter(void)
{
	TiXmlDeclaration dec("1.0","UTF-8","yes");
	m_pDoc->InsertEndChild(dec);
	TiXmlElement rootEle("Request");
	m_pRootNode = m_pDoc->InsertEndChild(rootEle);
}
XMLReqWriter::~XMLReqWriter(void)
{
}
int XMLReqWriter::write(const CommRequest& req,string& strReq)
{
	return doWrite(req,strReq);
}
/////////////////////////////////////////////////////////////////////
XMLRepWriter::XMLRepWriter(void)
{
	TiXmlDeclaration dec("1.0","UTF-8","yes");
	m_pDoc->InsertEndChild(dec);
	TiXmlElement rootEle("Result");
	m_pRootNode = m_pDoc->InsertEndChild(rootEle);
}


XMLRepWriter::~XMLRepWriter(void)
{
}
int XMLRepWriter::write(const CommReply& rep,string& strReply)
{
	return doWrite(rep,strReply);
}
